﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Mobileterminal.Common;
using Mobileterminal.Dtos.Yjb;
using Newtonsoft.Json;
using Sunny.UI;

namespace Mobileterminal.Forms.Yjb
{
    public partial class UpdateProgramInfo : UIEditForm
    {
        private ProgramInfoDTO app; // 使用你的DTO类
        public UpdateProgramInfo(string ProgramInfoId)
        {
            InitializeComponent();
            //LoadDataAsync(DeviceId);
            this.Load += async (sender, e) => await LoadDataAsync(ProgramInfoId);

        }


        private void UpdateProgramInfo_Load(object sender, EventArgs e)
        {

        }


        
        private async void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                app.ProgramName = uiTextBox7.Text;
              



                // 序列化并创建请求内容
                var json = JsonConvert.SerializeObject(app);
                HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");

                // 发送PUT请求
                var responseJson = await HttpClientHelper.ClientAsync(
                    "PUT",
                    "https://localhost:7286/api/Yjb/UpdateCX",  // 修正URL拼写
                    true,
                    httpContent
                );

                if (string.IsNullOrEmpty(responseJson))
                {
                    MessageBox.Show("服务器返回空响应，请检查服务端状态或请求数据。");
                    return;
                }

                // 反序列化响应
                var result = JsonConvert.DeserializeObject<DeviceDTO>(responseJson);
                if (result != null)
                {
                    MessageBox.Show("修改成功");
                }
                else
                {
                    MessageBox.Show("反序列化失败，响应内容格式不正确。");
                }
            }
            catch (HttpRequestException ex)
            {
                MessageBox.Show($"网络请求失败：{ex.Message}");
            }
            catch (JsonException ex)
            {
                MessageBox.Show($"解析响应失败：{ex.Message}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误：{ex.Message}");
            }
        }
        public static List<ProgramInfoDTO> appsTypes = new List<ProgramInfoDTO>();
        //private async void UpdateDevices_Load(object sender, EventArgs e)
        //{

        //}

        private async Task LoadDataAsync(string ProgramInfoId)
        {

            var url = $"https://localhost:7224/api/Show/Fantianprogram?ProgramInfoId={ProgramInfoId}";
            var json = HttpClientHelper.ClientAsync("GET", url);
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<ProgramInfoDTO>>>(await json);
            if (result != null && result.Data != null && result.Data.Count > 0)
            {
                app = result.Data[0];
                uiTextBox7.Text = app.ProgramName;
            }
        }
    }
}
